package com.isoft.permanprovidor.service.sgman;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.permanprovidor.bean.ResponseData;
import com.isoft.permanprovidor.bean.ResultCode;
import com.isoft.permanprovidor.bean.SysUser;
import com.isoft.permanprovidor.dao.sgman.ExterPersonContract_Dao;
import com.isoft.permanprovidor.entity.sgman.ExterPerson;
import com.isoft.permanprovidor.entity.sgman.ExterPersonContract;
import com.isoft.permanprovidor.unit.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

@Service
@Slf4j
public class ExterPersonContract_Service {
    @Autowired
    ExterPersonContract_Dao exterPersonContract_dao;


    /*分页综合查询操作*/
    public ResponseData PageSynthSelect(Integer currentPage , Integer size, String name ,String contractstartdate ,String contracttype){
        Page<ExterPersonContract> page = new Page<>();
        // 初始化分页参数
        if (currentPage == null || currentPage < 1){
            currentPage = 1;
        }
        if (size == null || size < 1){
            size =10;
        }

        // 构建查询条件
        page.setCurrent(currentPage).setSize(size);
        QueryWrapper wrapper = new QueryWrapper();
        //通过搜索姓名模糊查询
        if (!name.isEmpty()){
            wrapper.like("name" , name);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date contractstartdateDate = sdf.parse(contractstartdate);
            if (contractstartdate.isEmpty()){
                wrapper.eq("contractstartdate",contractstartdateDate);
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        if (!contracttype.isEmpty()){
            wrapper.like("contracttype" , contracttype);
        }
        wrapper.orderByAsc("id");

        //构造返回Controller 的结果
        ResponseData responseData = new ResponseData();
        responseData.setErrorCode(ResultCode.SUCCESS.getCode());
        responseData.setMsg(ResultCode.SUCCESS.getMessage());
        responseData.setData(exterPersonContract_dao.selectPage(page , wrapper));
        return responseData;
    }
    /*新增外聘人员合同*/
    public Boolean add(ExterPersonContract exterPersonContract){
        boolean result = false;
        if (exterPersonContract.getContractnumber().isEmpty()||exterPersonContract.getIdcard().isEmpty()) {
            return result;
        }
        exterPersonContract_dao.insert(exterPersonContract);
        result = true;
        return result;
    }
    /*修改外聘人员合同信息*/
    public Boolean update(ExterPersonContract exterPersonContract){
        if (exterPersonContract == null || !IdUtil.idCheck(exterPersonContract.getId())) {
            log.warn("信息为空或 ID 无效");
            return false;
        }
        try {
            int updateCount = exterPersonContract_dao.updateById(exterPersonContract);
            if (updateCount > 0) {
                log.info("外聘人员信息更新成功，ID: {}", exterPersonContract.getId());
                return true;
            } else {
                log.warn("外聘人员信息更新失败，ID: {}", exterPersonContract.getId());
                return false;
            }
        } catch (Exception e) {
            log.error("更新外聘人员信息时出错，ID: {}", exterPersonContract.getId(), e);
            throw new RuntimeException("更新信息时出错", e);
        }
    }
    /*启/停用*/
    public Boolean islock(Integer id ,String sysuserid  ,Integer islock){
        boolean result = false;
        // 参数校验：确保 id 和 sysuserid 不能为空，islock 也不能为 null
        if (id == null || sysuserid.isEmpty()) {
            log.warn("参数无效，id: {}, sysuserid: {}, islock: {}", id, sysuserid, islock);
            return result;
        }
        ExterPersonContract exterPersonContract = new ExterPersonContract();
        exterPersonContract.setIslock(islock);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id" , id);
        wrapper.eq("sysuserid" , sysuserid);

        // 执行更新操作
        exterPersonContract_dao.update(exterPersonContract, wrapper);
        result = true;
        return result;
    }


}
